package com.lindroy.androidanimation

import android.animation.*
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.animation.AnimationUtils
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_object_anim.*


/**
 * @author Lin
 * @date 2021/3/28
 * @function 属性动画
 * @Description
 */
class PropertyAnimActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_object_anim)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.property_anim, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.item_translate -> translate()
            R.id.item_scale -> scale()
            R.id.item_alpha -> alpha()
            R.id.item_rotate -> rotate()
            R.id.item_color -> color()
        }
        return true
    }

    /**
     * 平移
     */
    private fun translate() {
        /* val objectAnimator = AnimatorInflater.loadAnimator(this,R.animator.property_animator)
         objectAnimator.setTarget(button)
         objectAnimator.start()*/
        /* val oa = ObjectAnimator.ofFloat(button, "translationX", 0F, 100F)
         oa.duration = 1000
         oa.start()*/
        val valueAnimator = ValueAnimator.ofFloat(0F, 100F)
        valueAnimator.addUpdateListener {
            button.translationX = it.animatedValue as Float
        }
        valueAnimator.start()
    }

    /**
     * 旋转
     */
    private fun rotate() {
        val oa = ObjectAnimator.ofFloat(button, "rotationY", 0F, 360F)
        oa.duration = 1000
        oa.start()
    }

    /**
     * 放缩
     */
    private fun scale() {
        val oaX = ObjectAnimator.ofFloat(button, "scaleX", 0F, 1.5F, 1F)
        val oaY = ObjectAnimator.ofFloat(button, "scaleY", 0F, 1.5F, 1F)
        val set = AnimatorSet()
//        set.playSequentially(oaX,oaY) //动画依序执行
        set.playTogether(oaX, oaY) //所有动画同时执行
        set.duration = 1000
        set.setTarget(button)
        set.start()
        /* val oa = ObjectAnimator.ofFloat(button, "scaleX", 0F, 1.5F,1F)
         oa.duration = 1000
         oa.start()*/
    }

    /**
     * 透明
     */
    private fun alpha(): Unit {
        val oa = ObjectAnimator.ofFloat(button, "alpha", 0F, 1F)
        oa.duration = 1000
        oa.start()
    }

    /**
     * 颜色动画
     */
    private fun color() {
        val startColor = ContextCompat.getColor(this, R.color.button_bg)
        val oa = ObjectAnimator.ofArgb(
            button, "backgroundColor", startColor, Color.RED,
            Color.GREEN, startColor
        )
        oa.duration = 2000
        oa.start()
    }
}